#include "mc_param.h"
#include "mc_control.h"

/*
 *
 *       IB       IC     -->sector 1 current phase
 *IA              IC     -->sector 2 current phase
 *IA              IC     -->sector 3 current phase
 *IA     IB              -->sector 4 current phase
 *IA     IB              -->sector 5 current phase
 *       IB       IC     -->sector 6 current phase
 *adc1 get the first channel , adc2 get the second channel
 */
ADCParam_t motor_adc_param = {
    .adc1 = ADC1,
    .adc2 = ADC2,
#if USE_STM32F405
    .adc_trigger_edge = ADC_CR2_JEXTEN_0,
#endif
    .adc1_inject_table = {
        IB_CURR_CH << ADC_INJ_SHT,
        IA_CURR_CH << ADC_INJ_SHT,
        IA_CURR_CH << ADC_INJ_SHT,
        IA_CURR_CH << ADC_INJ_SHT,
        IA_CURR_CH << ADC_INJ_SHT,
        IB_CURR_CH << ADC_INJ_SHT,
    },
    .adc2_inject_table = {
        IC_CURR_CH << ADC_INJ_SHT,
        IC_CURR_CH << ADC_INJ_SHT,
        IC_CURR_CH << ADC_INJ_SHT,
        IB_CURR_CH << ADC_INJ_SHT,
        IB_CURR_CH << ADC_INJ_SHT,
        IC_CURR_CH << ADC_INJ_SHT,
    },
    .adc1_value = 0,
    .adc2_value = 0,
    .adc3_value = 0,
    .phase_a_offset = 1024,
    .phase_b_offset = 1024,
    .phase_c_offset = 1024,
};


McFocParam_t motor_foc_param = {
    .foc_timmer = TIM1,
    .ia = 0,
    .ib = 0,
    .ic = 0,
    .idq = {0},
    .idq_ref.d = 0,
    .idq_ref.q = 1500,
    .ialpha_beta = {0},
    .ualpha_beta = {0},
    .mc_uvw = {0},
    .theta = 0,
    .sa = 0,
    .sb = 0,
    .sc = 0,
    .low_duty = 0,
    .hight_duty = 0,
    .mid_duty = 0,
    .half_duty = 0,
    .pwm_duty_adc_time = TW_AFTER,//562, 1-562/5250= 0.9
    .ch4_cnt = MAX_DUTY - ADC_DEAD_TIME,
    .control_mod = torque_mode,

    .ia_norm = 0,
    .ib_norm = 0,
    .ic_norm = 0,

    .spd_ref = 3000,

};


EncoderData_t motor_enc_param = {
    .raw = 0,
    .encoder_count_now = 0,
    .encoder_count_pre = {0},
    .m_angle = 0,
    .e_angle = 0,
    .sector = SECTOR_4,
	.virtual_theta = 0,
};


PidHandle_t id_pid = {
    .def_kp_gain = 415,
    .def_ki_gain = 500,
    .kp_gain = 515,
    .ki_gain = 500,
    .upper_integral_limit = 32767*16384,
    .lower_integral_limit = -32767*16384,
    .upper_output_limit = 32767,
    .lower_output_limit = -32767,
    .prev_process_var_error = 0,
    .kp_divisor_pow2 = 14,
    .ki_divisor_pow2 = 14,
};

PidHandle_t iq_pid = {
    .def_kp_gain = 515,
    .def_ki_gain = 500,
    .kp_gain = 4915,
    .ki_gain = 500,
    .upper_integral_limit = 32767*16384,
    .lower_integral_limit = -32767*16384,
    .upper_output_limit = 32767,
    .lower_output_limit = -32767,
    .prev_process_var_error = 0,
    .kp_divisor_pow2 = 14,
    .ki_divisor_pow2 = 14,
};

PidHandle_t spd_pid = {
    .def_kp_gain = 14500,//14500/(2^12) = 3.54
    .def_ki_gain = 5000,//5000/(2^14) = 0.3f
    .kp_gain = 14500,//max 16384 = 1.0f
    .ki_gain = 5000,
    .upper_integral_limit = 32767*16384,//2^14 = 16384
    .lower_integral_limit = -32767*16384,//2^14 = 32767
    .upper_output_limit = 32767,
    .lower_output_limit = -32767,
    .prev_process_var_error = 0,
    .kp_divisor_pow2 = 12,
    .ki_divisor_pow2 = 14,
};




